package com.ilovepostcard.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ilovepostcard.PostcardActivity;
import com.ilovepostcard.R;
import com.ilovepostcard.model.Postcard;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.view.PostcardGridView.OnPostcardClickListener;
import com.mobclick.android.MobclickAgent;

public class PostcardDiscoverView2 extends LinearLayout {

    public PostcardDiscoverView2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate();
    }

    public PostcardDiscoverView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate();
    }

    public PostcardDiscoverView2(Context context) {
        super(context);
        inflate();
    }

    private ViewPager mPager;// 页卡内容
    private List<View> listViews; // Tab页面列表
//    private ImageView cursor;// 动画图片
    private List<PostcardTabItem> tabs;
//    private PostcardTabItem t1, t2;// 页卡头标
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
    
    PostcardGridView gridViewRecommend;
    PostcardGridView gridViewInterest;

    protected int getLayoutID() {
        return R.layout.discover_main_2;
    }
    
    protected int[] getTabIDs() {
        return new int[] {R.id.discover_tab_recommend, R.id.discover_tab_interest};
    }
    
    protected int[] getContentIDs() {
        return new int[] { R.layout.tab_content_recommend, R.layout.tab_content_interest };
    }
    
    protected View getContentView(int index) {
        return listViews.get(index);
    }
    
    protected void setupView() {
        gridViewRecommend = (PostcardGridView)getContentView(0).findViewById(R.id.discover_recommend);
        gridViewInterest = (PostcardGridView)getContentView(1).findViewById(R.id.discover_interest);
        gridViewRecommend.setOnPostcardClickListener(new OnPostcardClickListener() {

            @Override
            public void onClick(final Postcard postcard) {
                Intent intent = new Intent(getContext(), PostcardActivity.class);
                intent.putExtra("postcard", postcard);
                getContext().startActivity(intent);
            }
            
        });
        gridViewInterest.setOnPostcardClickListener(new OnPostcardClickListener() {

            @Override
            public void onClick(final Postcard postcard) {
            	MobclickAgent.onEvent(PostcardDiscoverView2.this.getContext(), Constants.COUNT_DISCOVER_WATCH);
                Intent intent = new Intent(getContext(), PostcardActivity.class);
                intent.putExtra("postcard", postcard);
                getContext().startActivity(intent);
            }
            
        });
        onPageChange(0);
    }
    
    private void inflate() {
        View view = View.inflate(getContext(), getLayoutID(), this);
        InitTabs();
        InitViewPager();
        InitImageView();
        setupView();
    }

    /**
     * 初始化头标
     */
    private void InitTabs() {
        tabs = new ArrayList<PostcardTabItem>();
        
        int[] tabIds = getTabIDs();
        for (int i = 0; i < tabIds.length; i++) {
            tabs.add((PostcardTabItem) findViewById(tabIds[i]));
        }
//        tabs.add((PostcardTabItem) findViewById(R.id.discover_tab_recommend));
//        tabs.add((PostcardTabItem) findViewById(R.id.discover_tab_interest));

        int size = tabs.size();
        for (int i = 0; i < size; i++) {
            tabs.get(i).setOnClickListener(new MyOnClickListener(i));
        }
    }

    
    
    /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
        mPager = (ViewPager) findViewById(R.id.vPager);
        listViews = new ArrayList<View>();
        int[] contentIds = getContentIDs();
        for (int i = 0; i < contentIds.length; i++) {
            listViews.add(View.inflate(getContext(), contentIds[i], null));
        }
//        listViews.add(View.inflate(getContext(), R.layout.tab_content_recommend, null));
//        listViews.add(View.inflate(getContext(), R.layout.tab_content_interest, null));
        mPager.setAdapter(new MyPagerAdapter(listViews));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
        
    }
    

    
    /**
     * 初始化动画
*/
    private void InitImageView() {
//        cursor = (ImageView) findViewById(R.id.cursor);
//        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.discover_topbar_scrollbar)
//                .getWidth();// 获取图片宽度
//        DisplayMetrics dm = new DisplayMetrics();
//        ((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
//        int screenW = dm.widthPixels;// 获取分辨率宽度
//        offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
//        Matrix matrix = new Matrix();
//        matrix.postTranslate(offset, 0);
//        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }

    /**
     * 头标点击监听
     */
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };
    
    /**
     * ViewPager适配器
*/
    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }
    
    protected void onPageChange(int page) {
        if (page == 0) {
            if (!gridViewRecommend.isLoaded())
                gridViewRecommend.loadNextPage();
        } else if (page == 1) {
        	MobclickAgent.onEvent(PostcardDiscoverView2.this.getContext(), Constants.COUNT_DISCOVER_FOLLOW);
            if (!gridViewInterest.isLoaded())
                gridViewInterest.loadNextPage();
        }
    }
    
    /**
     * 页卡切换监听
*/
    public class MyOnPageChangeListener implements OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
//        int two = one * 2;// 页卡1 -> 页卡3 偏移量

        @Override
        public void onPageSelected(int arg0) {
            int size = tabs.size();
            for (int i = 0; i < size; i++) {
                PostcardTabItem item = tabs.get(i);
                item.setHighlight(i == arg0);
            }
            
            onPageChange(arg0);
//            Animation animation = null;
//            switch (arg0) {
//            case 0:
//                animation = new TranslateAnimation(one, 0, 0, 0);
////                if (currIndex == 1) {
////                    animation = new TranslateAnimation(one, 0, 0, 0);
////                } else if (currIndex == 2) {
////                    animation = new TranslateAnimation(two, 0, 0, 0);
////                }
//                break;
//            case 1:
////                if (currIndex == 0) {
////                    animation = new TranslateAnimation(offset, one, 0, 0);
////                } else if (currIndex == 2) {
////                    animation = new TranslateAnimation(two, one, 0, 0);
////                }
//                animation = new TranslateAnimation(offset, one, 0, 0);
//                break;
////            case 2:
////                if (currIndex == 0) {
////                    animation = new TranslateAnimation(offset, two, 0, 0);
////                } else if (currIndex == 1) {
////                    animation = new TranslateAnimation(one, two, 0, 0);
////                }
////                break;
//            }
//            currIndex = arg0;
//            animation.setFillAfter(true);// True:图片停在动画结束位置
//            animation.setDuration(300);
//            cursor.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
